﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using ExpenseSystem.Common;
using ExpenseSystem.Core;
using ExpenseSystem.Fether;
using ExpenseSystem.Lor;
using ExpenseSystem.Model;

namespace ExpenseSystem.Core.Authority
{
    public class Authority
    {
        protected string tran = HttpContext.Current.Items[Definition.SESSION_TRAN_STR].ToString();

        public static UserProfile GetUserProfile()
        {
            UserProfile user = null;
            user = HttpContext.Current.Session[Definition.SESSION_LOGIN_STR] as UserProfile;
            if(user==null)
            {
                LogHelper.Info(typeof(Authority).Name,
                    String.Format("尝试从Session中取得用户信息失败:{0},{1}",HttpContext.Current.Request.Url.ToString(),HttpContext.Current.Request.UserHostAddress));
            }
            return user;
        }
        public bool GetAuthority(string authorityCode)
        {
            //权限代号定义方法:模块号_程序号_执行号
            if(String.IsNullOrWhiteSpace(authorityCode))
            {
                LogHelper.Auth(this.GetType().Name, "错误的权限代号");
                return false;
            }
            UserProfile user = null;
            authorityCode = authorityCode.ToUpper();
            try
            {
                user = GetUserProfile();
                LogHelper.Auth(GetType().FullName, String.Format("用户[{0}]/[{1}]尝试获得权限[{2}]",user.UserName,user.IpAddress,authorityCode));
            }
            catch(Exception ex)
            {
                LogHelper.Error(GetType().FullName,String.Format("无法获取登录信息:[{0}]",ex.Message));
                return false;
            }
            if(authorityCode.StartsWith("SYS"))
            {
                if (user.Admin)
                {
                    LogHelper.Auth(GetType().FullName, String.Format("用户[{0}]/[{1}]尝试获得权限[{2}]成功", user.UserName, user.IpAddress, authorityCode));
                    return true;
                }
            }
            RoleModuleFetcher roleModuleFetcher = new RoleModuleFetcher();
            string message;
            int rows;
            IList<RoleModuleModel> roleModuleModels;
            if (user.RoleList == null || user.RoleList.Count() == 0)
            {
                roleModuleModels = new List<RoleModuleModel>();
            }
            else
            {
                roleModuleModels = roleModuleFetcher.List(OrderBy.GetDefault(), Limit.GetDefault(), out message, out rows, Restrain.In("RoleId", user.RoleList).And(Restrain.Eq("Action", authorityCode)));
            }
            bool result = roleModuleModels.Count > 0;
            if(result)
                LogHelper.Auth(GetType().FullName, String.Format("用户[{0}]/[{1}]尝试获得权限[{2}]成功", user.UserName, user.IpAddress, authorityCode));
            else
                LogHelper.Auth(GetType().FullName, String.Format("ERROR:用户[{0}]/[{1}]尝试获得权限[{2}]失败", user.UserName, user.IpAddress, authorityCode));
            return result;
        }
    }
}
